// Ionic Starter App

// angular.module is a global place for creating, registering and retrieving Angular modules
// 'starter' is the name of this angular module example (also set in a <body> attribute in index.html)
// the 2nd parameter is an array of 'requires'
angular.module('starter', ['ionic', 'ngSanitize', 'btford.socket-io', 'main.controller', 'mydirective', 'test', 'quoteInput.controller', 'utilsService', 'invalidDirective', 'chat.controller'])

.run(function($ionicPlatform, $ionicHistory, $state, $rootScope, $q) {

    //返回promise
    $state.promises = {};


    //根据状态跳转到下一页 传参param
    $state.goForResult = function(stateName, param) {
        console.log("state go for result:" + $state.$current);
        var derfed = $q.defer();
        $state.promises[$state.$current] = derfed;
        // param = param{}
        //param.from =
        console.log("the current is " + $state.$current);
        $state.go(stateName, {
            from: $state.$current,
            data: param
        });
        return derfed.promise;
    }

    //stateName为返回的状态名 ，result为返回的数据
    $state.setResult = function(stateName, result) {
            var defered = $state.promises[stateName];
            defered.resolve(result);
            $ionicHistory.goBack();
        }
        //hisotry.goback()

    //for use

    $ionicPlatform.ready(function() {
        // Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
        // for form inputs)
        if (window.cordova && window.cordova.plugins.Keyboard) {
            cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
        }
        if (window.StatusBar) {
            StatusBar.styleDefault();
        }
    });
})

.config(function($stateProvider, $urlRouterProvider) {

    $stateProvider.state("login", {
            url: "/login",
            templateUrl: "templates/login.html",
            controller: "LoginController"

        })
        .state("setting", {
            url: "/setting",
            templateUrl: "templates/setting.html"
        })
        .state("home", {
            url: "/home",
            templateUrl: "templates/home.html",
            controller: "HomeController"
        })

    .state("testGetForResult", {
        url: "/testGetForResult/{from}/{data}",
        templateUrl: "templates/test/testGetForResult.html",
        controller: "GetForResultController"
    })

    .state("quote", {
        url: "/quote",
        templateUrl: "templates/quoteInput/mainQuoteInput.html",
        controller: "QuoteInputController"
    })

    .state("quote.platform", {
        url: "/platform",
        templateUrl: "templates/quoteInput/quotePlatform.html"
    })

    .state("quote.insureConfig", {
        url: "/insureConfig",
        templateUrl: "/templates/quoteInput/quoteInsureConfig.html",
        controller: "InsureConfigController"
    })

    .state("quote.provider", {
        url: "/provider",
        templateUrl: "/templates/quoteInput/quoteProvider.html"
    })

    .state("quote.input", {
        url: "/input",
        templateUrl: "/templates/quoteInput/quoteInput.html"
    })



    $urlRouterProvider.otherwise('/login');


})

.factory('socket', function(socketFactory) {
    //Create socket and connect to http://chat.socket.io 
    console.log("@@##factory socket");
    // var myIoSocket = io.connect('http://10.68.15.46:3000/');
    var myIoSocket = io.connect('192.168.0.101:3000/');

    mySocket = socketFactory({
        ioSocket: myIoSocket
    });

    return mySocket;
})
